package com.ch.dream.logic.shortstory;

import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.ch.dream.R;
import com.ch.dream.activity.shortstory.StoryContentActivity;
import com.ch.dream.asynctask.GetShortStoryRequestAsyncTask;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.dream.common.CommonData;
import com.ch.dream.common.CommonResult;
import com.ch.dream.common.CommonUserInfo;
import com.ch.dream.database.MyDatabase;
import com.ch.dream.tools.DateHelper;
import com.ch.dream.tools.ViewTools;

public class StoryContentLogic extends BaseLogic {
	public StoryContentLogic(StoryContentActivity activity) {
		super(activity);
	}
	public StoryContentLogic(StoryContentActivity activity,int layoutId) {
		super(activity,layoutId);
	}
	private TextView tvContent;
	private Button btnShare;
	private String content;
	private String storyId;
	
	public void initViews(){
		tvContent = (TextView)activity.findViewById(R.id.tvContent);
		btnShare = (Button)activity.findViewById(R.id.btnShare);
	}
	
	public void initData(){
		Intent intent =activity.getIntent();
		if(intent.getBooleanExtra("getStory", false)){
			
			JSONObject params = new JSONObject();
			try{
				params.put("userId",CommonUserInfo.userId);
			}catch (Exception e) {
				e.printStackTrace();
			}
			new GetShortStoryRequestAsyncTask(activity, new RequestServerListener() {
				@Override
				public void onRequestServerEnd(Object result) {
					try{
						progressDialog.dismiss();
						if(result!=null && ((JSONObject)result).get("result").equals(CommonResult.SUCCESS)){
							JSONObject jsonObject = (JSONObject) result;
							if(!jsonObject.has("story")){
								ViewTools.showShortToast(activity,"暂时没有更多故事了，试着自己创造一个吧");
								activity.finish();
								return;
							}
							JSONObject jsonStory = jsonObject.getJSONObject("story");
							content = jsonStory.getString("storyContent");
							tvContent.setText(content);
							storyId = jsonStory.getString("id");
							CommonData.gotShortStoryList.add(storyId);
//							int replyNum = UtilTools.getIntSharedPreferences(activity,CommonData.REPLYNUM, "getNum", 0);
//							replyNum = replyNum-1;
//							UtilTools.setIntSharedPreferences(activity,CommonData.REPLYNUM, "getNum", replyNum);
						}else if(result!=null && ((JSONObject)result).getString("result").equals(CommonResult.FAIL)){
							if(activity == CommonData.currentActivity){
								ViewTools.showShortToast(CommonData.currentActivity,  ((JSONObject)result).getString("message"));
								activity.finish();
							}
						}
					}catch (Exception e) {
						e.printStackTrace();
						ViewTools.showMessage(CommonData.currentActivity, "网络错误");
					}
					
				}
				
				@Override
				public void onRequestServerBegin() {
					progressDialog = ViewTools.showLoading(activity, "获取短故事", CommonData.LOADINGMESSAGE);
				}
			}).execute(params);
		}else{
			content = intent.getStringExtra("content");
			storyId = intent.getStringExtra("storyId");
		}
	}
	
	public void init(){
		initViews();
		initData();
		tvContent.setText(content);
		btnShare.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewTools.showShare(activity, "我做了一个梦：\n"+content+"\n来自故事梦android");
			}
		});
	}
	
	public void collect(){
		ContentValues values = new ContentValues();
		values.put("storyId", storyId);
		values.put("content", content);
		values.put("createTime", DateHelper.getNow());
		MyDatabase myDatabase = new MyDatabase(activity);
		try{
			myDatabase.insert(MyDatabase.shortStoryCollect, values);
			ViewTools.showShortToast(activity, "短故事收藏成功");
			activity.findViewById(R.id.titleRight).setVisibility(View.GONE);
		}catch (Exception e) {
			e.printStackTrace();
			ViewTools.showShortToast(activity, "数据错误，收藏失败");
		}finally{
			myDatabase.close();
		}
	}
}
